package com.zwr.sheller.customer.Service.Impl;

import com.zwr.sheller.core.common.WDMBusinessException;
import com.zwr.sheller.core.common.WDMExceptionCode;
import com.zwr.sheller.core.pojo.Goods;
import com.zwr.sheller.core.pojo.ShoppingCart;
import com.zwr.sheller.core.pojo.ShoppingCartDetail;
import com.zwr.sheller.core.pojo.Sku;
import com.zwr.sheller.customer.Service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;



import java.util.List;

import java.util.Set;

/**
 * @author PC
 */
@Service("shoppingCartService")
public class ShoppingCartServiceImpl implements ShoppingCartService {
    //购物车键名前缀
    public static final String SHOPPING_CART = "shoppingCart:";
    //购物车的商品元素键名前缀
    public static final String GOODS_ID = "goodsId:";
    //购物车的sku元素键名前缀
    public static final String SKU_ID = "skuId:";

    @Autowired
     private  RedisTemplate<String,Object> template = null;

    @Override
    public ShoppingCart selectShoppingCart(Integer userId) throws WDMBusinessException {

        //判断是否存在该购物车，
        Boolean aBoolean = template.hasKey(SHOPPING_CART + userId.toString());
        //创建一个购物车对象来存贮购物车信息
        ShoppingCart shoppingCart = new ShoppingCart();
        if (aBoolean){
            //获取购物车中所有sku的id
            Set<Object> skuIds = template.opsForHash().keys(SHOPPING_CART + userId.toString());
            //获取购物车中所有商品的数量
            List<Object> num =  template.opsForHash().values(SHOPPING_CART + userId.toString());
            for (Object filed : skuIds) {
                //创建一个购物车详情对象来存贮购物车详细信息
                ShoppingCartDetail shoppingCartDetail = new ShoppingCartDetail();

                /**
                 * 获取商品的名称
                 * 1.通过skuid获取sku的缓存
                 * 2.通过sku缓存中的goodsId获取goods的缓存
                 * 3.通过goods缓存获取goodsId
                 */
                Sku sku = (Sku)template.opsForValue().get(SKU_ID + filed);
                Goods goods =(Goods)template.opsForValue().get(GOODS_ID+sku.getGoodsId());
                shoppingCartDetail.setGoodsname(goods.getGoodsName());
                shoppingCartDetail.setImage(goods.getGoodsMainImg());
                shoppingCartDetail.setSkuname(sku.getSkuName());
                shoppingCartDetail.setSkuprice(sku.getSkuPrice());
                shoppingCartDetail.setSkunum(sku.getSkuNum());
                shoppingCartDetail.setSkuId(sku.getSkuId());
                shoppingCartDetail.setNum(Integer.valueOf( template.opsForHash().get(SHOPPING_CART + userId.toString(), filed).toString()));
                shoppingCart.getGoodsIdlist().add(shoppingCartDetail);
            }
            List<ShoppingCartDetail> goodsIdlist = shoppingCart.getGoodsIdlist();
            Double sum = null;
            for (ShoppingCartDetail sd : goodsIdlist) {
                 sum = sum + sd.getMoney();
            }
            shoppingCart.setTotalprice(sum);
            return shoppingCart;
        }
        throw new WDMBusinessException(WDMExceptionCode.SHOPPINGCART_NOT_EXIST);

    }

    @Override
    public void updateShoppingCartSkuNum(Integer userId, Long skuId, Integer num) {
        template.opsForHash().put(SHOPPING_CART + userId, skuId, num);
    }


    @Override
    public Boolean addShoppingCart(Integer userId,Long skuId,Integer num) throws WDMBusinessException {

        Boolean aBoolean =  template.opsForHash().putIfAbsent(SHOPPING_CART + userId, skuId, num);
        if (!aBoolean){
            throw new WDMBusinessException(WDMExceptionCode.GOODS_ALREADY_EXISTS);
        }
        return aBoolean;
    }

    @Override
    public void deleteShoppingCart(Integer userId, List<Long> skuIds) {
        Long delete =  template.opsForHash().delete(SHOPPING_CART + userId, skuIds);

    }

    @Override
    public Boolean cleanShoppingCart(Integer userId) throws WDMBusinessException {
        Boolean delete = template.delete(SHOPPING_CART + userId);
        if (!delete){
            throw  new WDMBusinessException(WDMExceptionCode.DELETE_FAILED);
        }
        return delete;
    }


}
